﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Konyvkolcsonzo
{
    public partial class FmKonyvek : FormKK
    {
        private List<konyv> nevsor;
        private Konyvek k;
        private enum listCtrl {irok, mufajok};

        public FmKonyvek()
        {
            InitializeComponent();
            nevsor = new List<konyv>();
            k = new Konyvek();
            this.pnlLeft.Controls.Add(txtKeres);
            this.pnlLeft.Controls.Add(lvwKonyvek);
            this.pnlLeft.Controls.Add(btnAdd);
            this.pnlLeft.Controls.Add(btnEdit);
            this.pnlLeft.Controls.Add(btnDelete);
            this.pnlLeft.Controls.Add(btnNewPeldany);
            this.pnlLeft.Controls.Add(btnClose);
            this.pnlLeft.Controls.Add(btnExit);
            this.pnlRight.Controls.Add(txbCim);
            this.pnlRight.Controls.Add(chlbIrok);
            this.pnlRight.Controls.Add(chlbMufajok);
            this.pnlRight.Controls.Add(btnOk);
            this.pnlRight.Controls.Add(btnCancel);
        }

        private void FmKonyvek_Load(object sender, EventArgs e)
        {
            Populate();
        }

        private void Populate()
        {
            PopulateMufajok();
            PopulateIrok();
            PopulateKonyvek();
        }

        private void PopulateIrok()
        {
            List<string> irok = k.getIrok();
            for (int i = 0; i < irok.Count; i++)
            {
                chlbIrok.Items.Add(irok[i]);
            }
        }

        private void PopulateMufajok()
        {
            List<string> mufaj = k.getMufajok();
            for (int i = 0; i < mufaj.Count; i++)
            {
                chlbMufajok.Items.Add(mufaj[i]);
            }
        }

        private void PopulateKonyvek()
        {
            lvwKonyvek.Items.Clear();
            nevsor = k.getKonyvek(txtKeres.Text);
            for (int i = 0; i < nevsor.Count; i++)
            {
                ListViewItem sor = new ListViewItem(Convert.ToString(nevsor[i].id));
                sor.SubItems.Add(Convert.ToString(nevsor[i].cim));
                sor.SubItems.Add(Convert.ToString(nevsor[i].irok));
                sor.SubItems.Add(Convert.ToString(nevsor[i].peldanyszam));
                sor.SubItems.Add(Convert.ToString(nevsor[i].mufajok));
                lvwKonyvek.Items.Add(sor);
                if (lvwKonyvek.SelectedItems.Count == 0 && lvwKonyvek.Items.Count > 0)
                {
                    lvwKonyvek.Items[0].Selected = true;
                }
                lvwKonyvek.Focus();
            }
        }

        private void PopulatePanelRight(long id)
        {
            konyv f = new konyv();
            f.cim = k.getKonyv(id);
            txbCim.Text = f.cim;
            ClearListbox((int)listCtrl.irok);
            ClearListbox((int)listCtrl.mufajok);
            string[] irok = lvwKonyvek.SelectedItems[0].SubItems[2].Text.Replace(", ", ",").Split(',');
            foreach (string iro in irok)
            {
                CheckListbox((int)listCtrl.irok, iro);
            } 
            string[] mufajok = lvwKonyvek.SelectedItems[0].SubItems[4].Text.Replace(", ", ",").Split(',');
            foreach (string mufaj in mufajok)
            {
                CheckListbox((int)listCtrl.mufajok, mufaj);
            }
        }

        private void PopulatePanelRight()
        {
            txbCim.Text = "";
            ClearListbox((int)listCtrl.irok);
            ClearListbox((int)listCtrl.mufajok);
        }

        private void CheckListbox(int control, string name) 
        {
            if (control == (int)listCtrl.irok) 
            {
                for (int i = 0; i < chlbIrok.Items.Count; i++)
                {
                    if (chlbIrok.Items[i].ToString() == name)
                    {
                        chlbIrok.SetItemChecked(i,true);
                    }
                }
            }
            else if (control == (int)listCtrl.mufajok)
            {
                for (int i = 0; i < chlbMufajok.Items.Count; i++)
                {
                    if (chlbMufajok.Items[i].ToString() == name)
                    {
                        chlbMufajok.SetItemChecked(i, true);
                    }
                }
            }
        }

        private void ClearListbox(int control)
        {
            if (control == (int)listCtrl.irok)
            {
                for (int i = 0; i < chlbIrok.Items.Count; i++)
                {
                    chlbIrok.SetItemChecked(i, false);
                }
            }
            else if (control == (int)listCtrl.mufajok)
            {
                for (int i = 0; i < chlbMufajok.Items.Count; i++)
                {
                    chlbMufajok.SetItemChecked(i, false);
                }
            }
        }

        private void txtKeres_TextChanged(object sender, EventArgs e)
        {
            PopulateKonyvek();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.cmdAdd();
            PopulatePanelRight();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (this.cmdEdit(lvwKonyvek.SelectedItems.Count))
            {
                PopulatePanelRight(Convert.ToInt64(lvwKonyvek.SelectedItems[0].SubItems[0].Text));
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.cmdCancel();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.cmdExit();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.cmdClose();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.cmdDelete(lvwKonyvek.SelectedItems.Count))
            {
                k.deleteKonyv(Convert.ToInt64(lvwKonyvek.SelectedItems[0].SubItems[0].Text));
                PopulateKonyvek();
            }
        }

        private void btnNewPeldany_Click(object sender, EventArgs e)
        {
            //this.cmdClose();
            FmPeldanyok f = new FmPeldanyok();
            f.setTitle("Példányok");
            f.setSmallSize();
            //f.Show();
            f.ShowDialog();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.cmdOK();
            konyv uj = new konyv();
            uj.cim = Convert.ToString(txbCim.Text);
            
            if (this.editType())
            {
                uj.id = Convert.ToInt64(lvwKonyvek.SelectedItems[0].SubItems[0].Text);
                List<string> irok = new List<string>();
                List<string> mufajok = new List<string>();
                foreach (Object o in chlbIrok.CheckedItems)
                {
                    irok.Add(o.ToString());
                } 
                foreach (Object o in chlbMufajok.CheckedItems)
                {
                    mufajok.Add(o.ToString());
                }
                k.updateKonyv(uj, irok, mufajok);
            }
            else
            {
                uj.cim = txbCim.Text;
                List<string> irok = new List<string>();
                List<string> mufajok = new List<string>();
                foreach (Object o in chlbIrok.CheckedItems)
                {
                    irok.Add(o.ToString());
                }
                foreach (Object o in chlbMufajok.CheckedItems)
                {
                    mufajok.Add(o.ToString());
                }
                k.insertKonyvek(uj, irok,mufajok);
            }
            PopulateKonyvek();
        }

        private void btnDelete_Click_1(object sender, EventArgs e)
        {
            if (this.cmdDelete(lvwKonyvek.SelectedItems.Count))
            {
                k.deleteKonyv(Convert.ToInt64(lvwKonyvek.SelectedItems[0].SubItems[0].Text));
                PopulateKonyvek();
            }
        }

        private void btnClose_Click_1(object sender, EventArgs e)
        {
            this.cmdClose();
        }

        private void btnExit_Click_1(object sender, EventArgs e)
        {
            this.cmdExit();
        }

        private void btnCancel_Click_1(object sender, EventArgs e)
        {
            this.cmdCancel();
        }

       


    }
}
